package com.zjx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjx.reggie.common.BaseContext;
import com.zjx.reggie.common.R;
import com.zjx.reggie.entity.User;
import com.zjx.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user, HttpServletRequest request){

        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.isNotEmpty(user.getPhone()),User::getPhone,user.getPhone());
        User one = userService.getOne(qw);

        if(one != null){
            request.getSession().setAttribute("userId",one.getId());
        }else{
            userService.save(user);
        }
        return R.success("登录成功！");
    }

    @PostMapping("/loginout")
    public R loginout(HttpServletRequest request){
        request.getSession().removeAttribute("userId");
        return R.success("退出成功");
}

}
